package a2022.a20221028;

/**
 * @author lenovo
 * @date 2022/10/28
 */
public class a {
    public static void main(String[] args) {
        System.out.println(sumSubarrayMins(
                new int[]{11, 81, 94, 43, 3}));
    }

    //11 81 91 43 3
    //11 81 43 3
    //11 43 3
    //11 3
    //3
//10^9 + 7
    //3，1，2，4，
    //1，1，2，
    //1，1，
    //1
    public static int sumSubarrayMins(int[] A) {
        long res = 0;
        long mod = 1000000007;

        for (int i = 0; i<A.length; i++) {
            int l = i-1;
            for (; l>=0 && A[i] < A[l]; l--) ;
            int r = i+1;
            for (; r<A.length && A[i] <= A[r]; r++) ;

            res += (i-l)*(r-i)*A[i];
        }
        return (int)(res % mod);
    }
}
